<!DOCTYPE html>
<html lang="cn">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客列表页</title>

    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/blog_list.css">

    <!--引入jQuery-->
    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
</head>

<body>
    <script>alert('hey！年轻的梦想家！')</script>
    <div class="nav">
        <img src="image/blog3.JPG">
        <div class="title">我的博客系统</div>
        <!-- 只是一个空白将后面的连接挤过去 -->
        <div class="space"></div>
        <a href="blog_list.html">主页</a>
        <a href="blog_edit.html">写博客</a>
        <a href="logout">注销</a>
    </div>

    <!-- 页面的主体部分 -->
    <div class="container">
        <!-- 左侧信息 -->
        <div class="container-left">
            <div class="card">
                <img src="./image/blog2.JPG" alt="">
                <h3></h3>
                <a href="https://www.github.com">github 地址</a>

                <div class="counter">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <div class="counter">
                    <span>2</span>
                    <span>1</span>
                </div>
            </div>
        </div>
        <!-- 右侧信息 -->
        <div class="container-right">
            <!--div class="blog">
                <div class="title">我的第一篇博客</div>
                <div class="date">2023-4-16 20:00:00</div>
                <div class="desc">
                    
                    从今天起，我要认真敲代码，Lorem ipsum dolor sit amet consectetur adipisicing elit. Tenetur, similique mollitia? Nostrum sit repellendus iure incidunt atque id? Quaerat suscipit molestias provident nihil dolorem officia eaque ea pariatur aut labore!

                </div>
                
                <a href="blog_detail.html?blogId=1">查看全文 &gt;&gt;</a>
            </div>-->
        </div>
    </div>

    <script>
        // 通过 ajax，给服务器发送请求
        function getBlogs() {
            $.ajax({
                type:'get',
                url:'blog',
                success: function(body){
                    let containerRight = document.querySelector('.container-right');
                    for (let blog of body){
                        let blogDiv = document.createElement('div');
                        blogDiv.className = 'blog';
                        let titleDiv = document.createElement('div');
                        titleDiv.className = 'title';
                        titleDiv.innerHTML = blog.title;
                        let dateDiv = document.createElement('div');
                        dateDiv.className = 'date';
                        dateDiv.innerHTML = blog.postTime;
                        let descDiv = document.createElement('div');
                        descDiv.className = 'desc';
                        descDiv.innerHTML = blog.content;
                        let a = document.createElement("a");
                        a.href = 'blog_detail.html?blogId=' + blog.blogId;
                        a.innerHTML = '查看全文 &gt;&gt;'

                        blogDiv.appendChild(titleDiv);
                        blogDiv.appendChild(dateDiv);
                        blogDiv.appendChild(descDiv);
                        blogDiv.appendChild(a);
                        containerRight.appendChild(blogDiv);
                    }
                }
            });
        }
        getBlogs();

        function getLoginStatus() {
            $.ajax({
                type: 'get',
                url: 'login',
                success: function(body){
                    console.log("用户已经登录");
                    let h3 = document.querySelector('.card h3');
                    // if (body.username == 'yutong'){
                    //     h3.innerHTML = '雨桐';
                    // }else{
                    //     h3.innerHTML = '云枫';
                    // }
                    h3.innerHTML = body.username;
                    if (body.username == 'Expire'){
                        let img = document.querySelector('.card img');
                        img.src = 'image/blog22.JPG';
                    }
                },
                error: function(body){
                    location.assign("blog.html"); 
                }
            });
        }
        getLoginStatus();
    </script>
</body>
</html>